package com.bascker.algorithm.practice.double_point.compare_version;

import java.util.ArrayList;
import java.util.List;

public class Solution1 {

    // 1 <= ver1.len, ver2.len <= 500
    public int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }

        // 按"."进行切割成修订号: 注意要转义
        String[] ver1 = version1.split("\\.");
        String[] ver2 = version2.split("\\.");

        // 修正2个修订号，保证数组长度一致
        int maxN = Math.max(ver1.length, ver2.length);
        List<String> verList1 = genVerList(ver1, maxN);
        List<String> verList2 = genVerList(ver2, maxN);

        // 逐个修订号进行比较
        for (int i = 0; i < maxN; i ++) {
            String fixNum1 = verList1.get(i);
            String fixNum2 = verList2.get(i);

            // 前导0都会被parseInt处理掉
            if (Integer.parseInt(fixNum1) < Integer.parseInt(fixNum2)) {
                return -1;
            } else if (Integer.parseInt(fixNum1) > Integer.parseInt(fixNum2)) {
                return 1;
            }
        }

        return 0;
    }

    private List<String> genVerList(String[] ver, int n) {
        List<String> verList = new ArrayList<>();

        for (int i = 0; i < n; i ++) {
            if (i < ver.length) {
                verList.add(ver[i]);
            } else {
                verList.add("0");
            }
        }

        return verList;
    }

}
